package cn.hr.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hr.dao.mapper.*;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.hr.pojo.Organization;
import cn.hr.service.OrganizationService;

/**
 * 组织结构服务接口实现类
 *
 * @author MO
 */
@CacheConfig(cacheNames = "organization")
@Scope("prototype")
@Service("organizationService")
public class OrganizationServiceImpl implements OrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private PostMapper postMapper;
    @Resource
    private PayrollOMapper payrollOMapper;
    @Resource
    private RecruitMapper recruitMapper;
    @Resource
    private WorMapper worMapper;

    @Override
    public boolean insert(Organization t) throws Exception {
        t.setCode("ORGAN_CODE" + (organizationMapper.findMaxId() + 1));
        return organizationMapper.insert(t) > 0;
    }

    @Override
    public boolean update(Organization t) throws Exception {
        return organizationMapper.update(t) > 0;
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        return false;
    }
    @CacheEvict(allEntries = true)
    public String deleteById(Integer id) throws  SQLException{
        String result = "false";
        Map<String,Object> map = new HashMap<>();
        map.put("oId",id);
        map.put("parentId",id);
        if (userMapper.findCountByCondition(map)==0&&postMapper.findCountByCondition(map)==0
                &&payrollOMapper.findCountByCondition(map)==0&&recruitMapper.findCountByCondition(map)==0
                &&worMapper.findCountByCondition(map)==0&&organizationMapper.findCountByCondition(map)==0) {
            if ( organizationMapper.delete(id) > 0){
                result = "true";//删除成功
            }
        }else{
            result = "isCount";
        }
        return result;
    }

    @Override
    public Organization get(Integer id) throws Exception {
        return organizationMapper.get(id);
    }

    @Override
    public PageInfo<Map> findListByCondition(Map m, int pageNo) throws Exception {
        PageHelper.startPage(pageNo, 0);
        List<Map> list = organizationMapper.findListByCondition(m);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Long findCountByCondition(Map m) throws Exception {
        return null;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<Organization> findAll() throws SQLException {
        return organizationMapper.findAll();
    }

    @Override
    public List<Map> findSubdivisionByParentId(Map map) throws Exception {
        List<Map> organizationList = new ArrayList<>();
        List<Map> list1 = organizationMapper.findListByCondition(map);//获取一级组织
        for (Map map1 : list1) {
            map.put("parentId", map1.get("id"));
            List<Map> list2 = organizationMapper.findListByCondition(map);//获取二级组织
            for (Map map2 : list2) {
                map.put("parentId", map2.get("id"));
                List<Map> list3 = organizationMapper.findListByCondition(map);//获取三级组织
                organizationList.addAll(list3);
            }
        }
        return organizationList;
    }
}
